package com.example.controller;

import com.example.entity.Result;
import com.example.service.AdminService;
import com.example.utils.ResultUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

/**
 * @author John
 */
@RequestMapping("/Admin")
@RestController
public class AdminController {

    @Resource
    private AdminService adminService;

    @ApiOperation(value = "登录验证信息")
    @PostMapping("/checkAdmin")
    public ModelAndView checkAdmin(@RequestParam("id")String id , @RequestParam("password")String password, HttpServletResponse response) {
        String path = "student/index";
        ModelAndView mav = adminService.loginCheck(id, password);
        if (path.equals(mav.getViewName())) {
            Cookie cookie = new Cookie("id", id);
            cookie.setMaxAge(600);
            response.addCookie(cookie);
            return mav;
        } else {
            return mav;
        }
    }

//    @PostMapping("/shiro")
//    public Object shiro(String username,String password){
//        UsernamePasswordToken token = new UsernamePasswordToken(username,password);
//        char[] password1 = token.getPassword();
//        String s = password1.toString();
//        SecurityUtils.getSubject().login(token);
//        token.setRememberMe(true);
//        return 1;
//    }






    @PutMapping("/updateAdmin")
    public Result updateAdmin(@RequestParam ("id")String id, @RequestParam ("password")String password) {
        return ResultUtils.success(adminService.updatepassword(id,password));
    }


}
